home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / String Extractor⁄Localization / Scanner / Literal.c < prev    next >
Text File  |  1993-06-18  |  3KB  |  143 lines

  1. /*
  2.  * Literal.c
  3.  */
  4.  
  5. #include "Literal.h"
  6.  
  7.  
  8. #define DEFAULT_SIZE 8
  9.  
  10.  
  11. typedef struct MajorLine {
  12.     short id ;
  13.     short numStrings ;
  14.     char * * stringData ;
  15.     char * * stringStarts ;
  16. } MajorLine ;
  17.  
  18.  
  19. MajorLine * cache = NULL ;
  20. short cacheSize = 0 ;
  21. short cacheBase = 0 ;
  22.  
  23.  
  24. static void
  25. LoadID ( short id , MajorLine * where ) {
  26.  
  27. Handle h ;
  28. unsigned char * iterator ;
  29. unsigned char * from ;
  30. short count ;
  31.  
  32.     if ( where -> stringData ) {
  33.         DisposeHandle ( where -> stringData ) ;
  34.     }
  35.     if ( where -> stringStarts ) {
  36.         DisposePtr ( ( Ptr ) where -> stringStarts ) ;
  37.     }
  38.     h = GetResource ( 'STR#' , id ) ;
  39.     DetachResource ( h ) ;
  40.     where -> numStrings = * ( short * ) * h ;
  41.     where -> stringStarts = ( char * * ) NewPtrClear ( sizeof ( char * ) *
  42.         where -> numStrings ) ;
  43.     where -> id = id ;
  44.     SetHandleSize ( h , GetHandleSize ( h ) + where -> numStrings ) ;
  45.     HLockHi ( h ) ;
  46.     where -> stringData = h ;
  47.  
  48.     count = 0 ;
  49.     iterator = ( unsigned char * ) * h + 2 ;
  50.     while ( count < where -> numStrings ) {
  51.         where -> stringStarts [ count ] = ( char * ) iterator ;
  52.         iterator += * iterator + 1 ;
  53.         count ++ ;
  54.     }
  55.     from = iterator - 1 ;
  56.     iterator += count - 1 ; /* Last in buffer */
  57.     while ( count -- ) {
  58.  
  59.     int cnt = ( * ( unsigned char * ) where -> stringStarts [ count ] ) + 1 ;
  60.  
  61.         * ( iterator -- ) = 0 ;
  62.         while ( cnt -- ) {
  63.             * ( iterator -- ) = * ( from -- ) ;
  64.         }
  65.         where -> stringStarts [ count ] += count ;
  66.     }
  67. }
  68.  
  69.  
  70. static Boolean
  71. InitCache ( short size ) {
  72.     if ( cache ) {
  73.         SysBeep ( 20 ) ;
  74.         return 0 ;
  75.     }
  76.     cacheSize = size ;
  77.     cache = ( MajorLine * ) NewPtrClear ( sizeof ( MajorLine ) * size ) ;
  78.  
  79.     return 1 ;
  80. }
  81.  
  82.  
  83. void
  84. InitStringTable ( short id ) {
  85.  
  86. short count , idSave = id , pos ;
  87. Handle h ;
  88.  
  89.     cacheBase = id ;
  90.     if ( cache ) {
  91.         SysBeep ( 20 ) ;
  92.         return ;
  93.     }
  94.  
  95.     SetResLoad ( 0 ) ;
  96.     while ( h = GetResource ( 'STR#' , id ) ) {
  97.         ReleaseResource ( h ) ;
  98.         id ++ ;
  99.         count ++ ;
  100.     }
  101.     SetResLoad ( 1 ) ;
  102.  
  103.     InitCache ( count ) ;
  104.  
  105.     for ( id = idSave ; id < idSave + count ; id ++ ) {
  106.         pos = ( ( id - cacheBase ) & 0x7fff ) % cacheSize ;
  107.         LoadID ( id , cache + pos ) ;
  108.     }
  109. }
  110.  
  111.  
  112. char *
  113. CLiteral ( short id , short num ) {
  114.  
  115. short pos ;
  116.  
  117.     if ( ! cache ) {
  118.         if ( ! InitCache ( DEFAULT_SIZE ) ) {
  119.             return NULL ;
  120.         }
  121.     }
  122.     pos = ( ( id - cacheBase ) & 0x7fff ) % cacheSize ;
  123.     if ( ! ( cache [ pos ] . id == id ) ) {
  124.         LoadID ( id , cache + pos ) ;
  125.     }
  126.     if ( num < 1 || num > cache [ pos ] . numStrings ) {
  127.         return NULL ;
  128.     }
  129.     return cache [ pos ] . stringStarts [ num - 1 ] + 1 ; /* C string */
  130. }
  131.  
  132.  
  133. unsigned char *
  134. PLiteral ( short id , short num ) {
  135.  
  136. unsigned char * ret = ( unsigned char * ) CLiteral ( id , num ) ;
  137.  
  138.     if ( ret ) {
  139.         return ret - 1 ; /* Adjust for pascal string */
  140.     }
  141.     return ret ;
  142. }
  143.